#!/usr/bin/perl
use strict;
use FindBin;
use Getopt::Long;

sub usage {
    my $pname = $FindBin::Script;

    print("Usage: $pname --name ContanerName --image ImageName --tag tagname\n");
    print("\n");
    print("       --name:    ContainerName\n");
    print("       --image:   Image name\n");
    print("       --tagname: Tag name.\n");
    print("       arg1 arg2....argN");

    exit(-1);
}

sub main {
    my ( $name, $image, $tagname, $ports , $network );

    GetOptions(
        'name:s'    => \$name,
        'image:s'   => \$image,
        'tagname:s' => \$tagname,
        'ports:s' => \$ports,
        'network:s' => \$network
    );

    if (   not defined($name)
        or not defined($image)
        or not defined($tagname) )
    {
        usage();
    }

    $image   = lc($image);
    $tagname = lc($tagname);

    #TODO:这里的docker命令是在远程OS上运行，需要确认是否需要sudo docker
    my $cmd = "sudo docker rm -vf $name";
    print("INFO: Execute->$cmd\n");
    my $ret = system($cmd );

    if ( $ret eq 0 ) {
        print("INFO: Container $name is removed.\n");
    }
    else {
        print("INFO: Container $name is not running.\n");
    }

    my $cmd = "sudo docker run -d --name=$name";
    if( defined($ports) and  $ports ne '' ){
        my @portList = split( /,/, $ports );
        foreach my $port (@portList){
            if( $port ne '' ){
                $cmd = $cmd . " -p $port";
            }
        }
    }

    if( defined($network) and  $network ne '' ){
        $cmd = $cmd . " --net $network";
    }
    $cmd = $cmd . " @ARGV $image:$tagname";

    #docker --tlsverify --tlscacert=/app/ezdeploy/certs/ca.pem --tlscert=/app/ezdeploy/certs/cert.pem --tlskey=/app/ezdeploy/certs/key.pem -H $host:$h_port logs -f $name";
    print("INFO: Execute->$cmd\n");
    $ret = system($cmd );

    if ( $ret eq 0 ) {
        print("INFO: Docker run success.\n");
    }
    else {
        print("ERROR: Docker run failed.\n");
    }

    return ($ret);
}

exit main();
